// Register page module
// ====================

define(['zepto', 'sui', 'submitForm'], function($) {
  return {
    init: function(data) {
      this.batchId = data.batchId;
      this.dailyId = data.dailyId;
      this.actionAmount = data.actionAmount;
      this.isSignin = data.isSignin;
      this.monthlyId = data.monthlyId;
      
      this.currentId = 1;
      this.initPage();
//      this.bindUpDownBtn();
      this.bindPrevNextBtn();
    },
    initPage: function() {
      var that = this;
      $(".end-btn").click(function() {
        $.ajax({
          url: window.__CTX__.root + "/courses/" + that.monthlyId + "/batches/" + that.batchId + "/units/" + that.dailyId + "/complete",
          type: "POST",
          success: function(res) {
            res = JSON.parse(res);
            if(res.code == 0) {
              window.location.href = window.__CTX__.root + "/courses/" + that.monthlyId + "/batches/" + that.batchId + "/units/" + that.dailyId + "/result";
            } else {
              $.toast(res.message);
            }
          },
          failure: function() {
            $.toast("请求失败 ");
          }
        }) 
      })
      
    },
    /*bindUpDownBtn: function() {
      $(document).on("click", ".updown-btn", function(e) {
        e.preventDefault();
        if($(this).hasClass("icon-up")) {
          $(this).removeClass("icon-up").addClass("icon-down");
          $(this).parents(".details").find(".description").show();
        } else {
          $(this).removeClass("icon-down").addClass("icon-up");
          $(this).parents(".details").find(".description").hide();
        }
      })
    },*/
    bindPrevNextBtn: function() {
      var that = this;
      
      if(that.actionAmount > 1) {
    	  that.bindActionBtnClick();
      }
      
    },
    
    bindActionBtnClick: function() {
    	var that = this;
    	
    	$(document).on("click", ".ft-button-yellow", function() {
    	      
            $(this).data("action") == 'next' ? that.currentId++ : that.currentId > 1 ? that.currentId-- : '';
            $("[data-id='" + that.currentId + "']").show().siblings(".homework-card").hide();
            var titleText = $("[data-id='" + that.currentId + "']").data("name")
            $("title").html(titleText);
            
            (that.isSignin != "true" && that.currentId == that.actionAmount) ? $(".end-btn").removeClass("hide") : $(".end-btn").addClass("hide");
            (that.isSignin == "true" && that.currentId == that.actionAmount) ? $(".signin-result").removeClass("hide") : $(".signin-result").addClass("hide");
            that.currentId == 1 ? $(".button-wrap").find("[data-action='prev']").addClass("hide") : $(".button-wrap").find("[data-action='prev']").removeClass("hide");
            that.currentId == that.actionAmount ? $(".button-wrap").find("[data-action='next']").addClass("hide") : $(".button-wrap").find("[data-action='next']").removeClass("hide");
          })
    }
   
  }
})